package com.example.thread;


import com.alibaba.fastjson.JSON;

import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.StampedLock;

public class ThreadTest {

    public static void main(String[] args) {
        final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1000, 1000, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));
        long l = System.currentTimeMillis();
        Map<String, Integer> map = new ConcurrentHashMap<>();
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                if (map.containsKey(Thread.currentThread().getName())) {
                    // System.out.println(Thread.currentThread().getName());
                }
                map.put(Thread.currentThread().getName(), 1);
                String a = String.valueOf(finalI);
                System.out.println(JSON.toJSONString(Thread.currentThread().getName()) + "在执行");
                countDownLatch.countDown();
            });
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Integer[] aaa = {3, 2, 1};
        long l2 = System.currentTimeMillis();
        System.out.println("l2-l1----" + (l2 - l));

        for (int i = 0; i < 100; i++) {
            String a = String.valueOf(i);
            //System.out.println("我在执行"+a);
        }
        long l3 = System.currentTimeMillis();
        // System.out.println("l3-l2----"+(l3-l2));
        PriorityQueue queue = new PriorityQueue();
        queue.add(5);
        queue.add(4);
        queue.add(6);
        queue.add(3);
        queue.comparator();
        System.out.println(queue);
    }
}


class Point {
    private final StampedLock sl = new StampedLock();
    private double x, y;

    public static void main(String[] args) {
        LongAdder longAdder = new LongAdder();
        longAdder.increment();
        System.out.println(longAdder.longValue());
        long a = 10;
        float b = 10;
        a = (long) b;
        Set<Integer> set = new HashSet();
        set.add(2);
        set.add(1);
        set.add(2);
        Object[] ada = new ArrayList<Integer>().toArray();
        System.out.println(set.contains(3));
        System.out.println(set);
        Connection con = null;

        Statement statement = null;
        try {
            statement = con.createStatement();
            statement.setFetchSize(20000);
            ResultSet resultSet = statement.executeQuery("");
            while (resultSet.next()) {
                ResultSetMetaData metaData = resultSet.getMetaData();
                metaData.getColumnCount();

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    void move(double deltaX, double deltaY) { // an exclusively locked method
        long stamp = sl.writeLock();
        try {
            x += deltaX;
            y += deltaY;
        } finally {
            sl.unlockWrite(stamp);
        }
    }

    double distanceFromOrigin() { // A read-only method
        long stamp = sl.tryOptimisticRead();
        double currentX = x, currentY = y;
        if (!sl.validate(stamp)) {
            stamp = sl.readLock();
            try {
                currentX = x;
                currentY = y;
            } finally {
                sl.unlockRead(stamp);
            }
        }
        return Math.sqrt(currentX * currentX + currentY * currentY);
    }

    void moveIfAtOrigin(double newX, double newY) { // upgrade
        // Could instead start with optimistic, not read mode
        long stamp = sl.readLock();
        try {
            while (x == 0.0 && y == 0.0) {
                long ws = sl.tryConvertToWriteLock(stamp);
                if (ws != 0L) {
                    stamp = ws;
                    x = newX;
                    y = newY;
                    break;
                } else {
                    sl.unlockRead(stamp);
                    stamp = sl.writeLock();
                }
            }
        } finally {
            sl.unlock(stamp);
        }
    }
}


class CompletableFutureDemo{
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1000, 1000, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));


    public static void main(String[] args) {
        SmallTool.printTimeAndThread("小白进入餐厅");
        SmallTool.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");

        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("厨师炒菜");
            SmallTool.sleepMillis(200);
            return "番茄炒蛋";
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("服务员蒸饭");
            SmallTool.sleepMillis(300);
            return "米饭";
        }), (dish, rice) -> {
            SmallTool.printTimeAndThread("服务员打饭");
            SmallTool.sleepMillis(100);
            return String.format("%s + %s 好了", dish, rice);
        });
        System.out.println(cf1.join());
        SmallTool.printTimeAndThread("小白在打王者");
        SmallTool.printTimeAndThread(String.format("%s ,小白开吃", cf1.join()));


    }
    static class SmallTool {

        public static void sleepMillis(long millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static void printTimeAndThread(String tag) {
            String result = new StringJoiner("\t|\t")
                    .add(String.valueOf(System.currentTimeMillis()))
                    .add(String.valueOf(Thread.currentThread().getId()))
                    .add(Thread.currentThread().getName())
                    .add(tag)
                    .toString();
            System.out.println(result);
        }

    }

}